/*
 *   Copyright 2011-2012 Hai Bison
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package group.pals.mailgate.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Timer;

/**
 *
 * @author Hai Bison
 */
public final class MouseHandler extends MouseAdapter {

    private final List<MouseListener> Listeners;
    private final Timer _Timer;
    private MouseEvent mouseEvent;

    public MouseHandler(int doubleClickDelayTime) {
        this.Listeners = new ArrayList<MouseListener>();
        if (doubleClickDelayTime < 100 || doubleClickDelayTime > 1000) {
            doubleClickDelayTime = 300;
        }
        this._Timer = new Timer(doubleClickDelayTime, null);
        this._Timer.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                for (MouseListener listener : Listeners) {
                    listener.mouseSingleClicked(mouseEvent);
                }
            }
        });
        this._Timer.setRepeats(false);
    }

    public void addMouseListener(MouseListener listener) {
        if (!Listeners.contains(listener)) {
            Listeners.add(listener);
        }
    }

    public void removeMouseListener(MouseListener listener) {
        Listeners.remove(listener);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        for (MouseListener listener : Listeners) {
            listener.mouseClicked(e);
        }

        if (_Timer.isRunning()) {
            _Timer.stop();
            for (MouseListener listener : Listeners) {
                listener.mouseDoubleClicked(e);
            }
        } else {
            mouseEvent = e;
            _Timer.start();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        for (MouseListener listener : Listeners) {
            listener.mousePressed(e);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        for (MouseListener listener : Listeners) {
            listener.mouseReleased(e);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        for (MouseListener listener : Listeners) {
            listener.mouseEntered(e);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        for (MouseListener listener : Listeners) {
            listener.mouseExited(e);
        }
    }

    /**
     * @return the doubleClickDelayTime
     */
    public int getDoubleClickDelayTime() {
        return _Timer.getInitialDelay();
    }

    /**
     * @param doubleClickDelayTime the doubleClickDelayTime to set
     */
    public void setDoubleClickDelayTime(int doubleClickDelayTime) {
        if (doubleClickDelayTime < 100 || doubleClickDelayTime > 1000) {
            doubleClickDelayTime = 300;
        }
        this._Timer.setInitialDelay(doubleClickDelayTime);
    }
}
